package com.itheima.dubbo.api;

import com.itheima.domain.mongo.Sound;
import com.itheima.dubbo.util.IdSequence;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;
import java.util.List;

/**
 * @author 阿鹏
 * @date 2021/2/25 1:35
 */
@Service
public class SoundApiImpl implements SoundApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 当用户接受完语音之后,要把语音删掉
     * @param id
     */
    @Override
    public void delete(ObjectId id) {
        Query query = new Query(Criteria.where("id").is(id));
        mongoTemplate.remove(query,Sound.class);
    }

    /**
     * 查询所有语音
     * @return
     */
    @Override
    public List<Sound> findSoundList() {

        return mongoTemplate.findAll(Sound.class);

    }

    /**
     * 保存语音
     * @param sound
     */
    @Override
    public void save(Sound sound) {
        Date now = new Date();
        sound.setCreated(now);
        sound.setId(new ObjectId());
        mongoTemplate.save(sound);
    }
}
